from app_base import *
from settings import *
from web_element import *
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException


class PoHome(PoBase):

    # 小红书的id属性根据不同版本是动态不相同的，所以优先使用class或xpath的方式定位元素

    def bottom_bar_click(self, text):  # 底栏按钮
        xpath = '//android.widget.TextView[@text="{}"]'.format(text)
        self.elm_wait_select(20, By.XPATH, xpath).click()
        logger.debug("底栏按钮》{}《".format(text))
        
    def bottom_search_angela(self):
        self.elm_wait_select(50, By.ID, 'com.xingin.xhs:id/brx').click()  # brx
        logger.info("点击搜索按钮")
        self.elm_wait_select(5, By.CLASS_NAME, 'android.widget.EditText').set_value("angela🍃")
        self.elms_wait_select(10, By.ID, 'com.xingin.xhs:id/ao6')[0].click() # android.widget.LinearLayout

    def bottom_search(self, keyword):  # 首页顶部搜索按钮
        self.elm_wait_select(50, By.ID, 'com.xingin.xhs:id/brx').click()  # brx
        logger.info("点击搜索按钮")
        self.elm_wait_select(5, By.CLASS_NAME, 'android.widget.EditText').set_value(keyword)  # 输入关键词并回车
        self.elm_wait_select(10, By.ID, 'com.xingin.xhs:id/ayf').click()
        # self.elm_wait_select(3, By.XPATH, '//android.widget.TextView[@text="搜索"]').click()
        logger.info("搜索输入框输入小红书号：{}".format(keyword))
        time.sleep(1)
        # xpath = "//androidx.appcompat.app.ActionBar.Tab[3]/android.widget.TextView"
        # self.elm_wait_select(5, By.XPATH,xpath).click()
        
        # self.elm_wait_select(3, By.XPATH, "//android.widget.TextView[@text='用户']").click()
        self.elms_wait_select(3, By.CLASS_NAME, "androidx.appcompat.app.ActionBar$Tab")[1].click()
        logger.info("切换至用户标签")
        time.sleep(1)
        logger.debug("等待加载")
        self.elm_wait_select(10, By.ID, 'com.xingin.xhs:id/ayl').click()
        # self.elm_wait_select(10, By.XPATH, '//android.widget.TextView[@text="小红书号：{}"]'.format(keyword)).click()
        logger.info("选中用户：{}".format(keyword))

    def views(self):  # 信息流列表
        views = self.elms_wait_select(3, By.ID, 'com.xingin.xhs:id/c_j')
        logger.debug("视图列表")
        return views
    
    def video_views(self):  # 信息流列表
        views = self.elms_select(By.ID, 'com.xingin.xhs:id/ah5')
        logger.debug("视频列表")
        return views

    def back_button(self):  # 图文页返回按钮
        elm = self.elm_wait_select(3, By.ID, 'com.xingin.xhs:id/gr')  # 精简版：gl 标准版：sp
        logger.debug("图文页返回按钮")
        return elm

    def back_button_vedio(self):  # 视频页返回按钮
        elm = self.elm_wait_select(3, By.ID, 'com.xingin.xhs:id/backButton')
        logger.debug("视频页返回按钮")
        return elm

    # 图文页按钮

    def h1_text(self):  # 图文标题
        h1_text = self.elm_wait_select(3, By.ID, 'com.xingin.xhs:id/bb7').text  # 精简版：acm 标准版：unknow
        logger.debug("页面标题:{}".format(h1_text))
        return h1_text

    def vedio_h1_text(self):  # 视频标题
        vedio_h1_text = self.elm_wait_select(10, By.ID, 'com.xingin.xhs:id/noteContentText').text
        # logger.debug("页面标题:{}".format(vedio_h1_text))
        return vedio_h1_text

    def main_text(self):  # 正文
        main_text = self.elm_wait_select(3, By.ID, 'com.xingin.xhs:id/ad2').text  # 精简版：acm 标准版：unknow
        logger.debug(main_text)
        return main_text
        
    def is_img(self):
        try:
            self.elm_wait_select(2, By.ID, 'com.xingin.xhs:id/gr')
            logger.debug("图文页面")
            return True
        except TimeoutException:
            logger.debug("视频页面")
            return False


    def content_head_follow(self):  # 🌊
        elm = self.elm_wait_select(3, By.ID, 'com.xingin.xhs:id/followTV')
        logger.debug("🌊关注状态为{}".format(elm.get_attribute("selected")))
        return elm

    def content_tail_like(self):  # 💗
        xpath = "//android.view.ViewGroup/android.view.ViewGroup/android.widget.LinearLayout[1]/android.widget.ImageView"
        elm = self.elm_wait_select(3, By.XPATH, xpath)
        logger.debug("💗喜欢状态为{}".format(elm.get_attribute("selected")))
        return elm

    def content_tail_favorites(self):  # 🌟
        xpath = "//android.view.ViewGroup/android.view.ViewGroup/android.widget.LinearLayout[2]/android.widget.ImageView"
        elm = self.elm_wait_select(3, By.XPATH, xpath)
        logger.debug("🌟收藏状态为{}".format(elm.get_attribute("selected")))
        return elm

    def content_tail_comment(self):  # 💬
        xpath = "//android.view.ViewGroup/android.view.ViewGroup/android.widget.LinearLayout[3]/android.widget.ImageView"
        elm = self.elm_wait_select(3, By.XPATH, xpath)
        logger.debug("💬收藏状态为{}".format(elm.get_attribute("selected")))
        return elm

    # 其他杂项

    # def share_text_code(self, text_code):
    #     self.set_clipboard_text(text_code.encode())


def main():
    pass


if __name__ == '__main__':
    main()
